package com.youyi.yyviewsdklibrary.picker.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.lkx.liandianqinew.R;
import com.youyi.yyviewsdklibrary.picker.contract.OnTimeMeridiemSelectedListener;
import com.youyi.yyviewsdklibrary.picker.contract.OnTimeSelectedListener;
import com.youyi.yyviewsdklibrary.picker.contract.TimeFormatter;
import com.youyi.yyviewsdklibrary.picker.contract.WheelFormatter;
import com.youyi.yyviewsdklibrary.picker.entity.TimeEntity;
import com.youyi.yyviewsdklibrary.picker.impl.UnitTimeFormatter;
import java.util.Arrays;
import java.util.List;

public class TimeWheelLayout extends BaseWheelLayout {
    private TimeEntity defaultValue;
    private TimeEntity endValue;
    private TextView hourLabelView;
    private int hourStep = 1;
    private NumberWheelView hourWheelView;
    private boolean isAnteMeridiem;
    private TextView minuteLabelView;
    private int minuteStep = 1;
    private NumberWheelView minuteWheelView;
    private OnTimeMeridiemSelectedListener onTimeMeridiemSelectedListener;
    private OnTimeSelectedListener onTimeSelectedListener;
    private boolean resetWhenLinkage = true;
    private TextView secondLabelView;
    private int secondStep = 1;
    private NumberWheelView secondWheelView;
    private Integer selectedHour;
    private Integer selectedMinute;
    private Integer selectedSecond;
    private TimeEntity startValue;
    private int timeMode;

    public TimeWheelLayout(Context context) {
        super(context);
    }

    public TimeWheelLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public TimeWheelLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    public TimeWheelLayout(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i, i2);
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public int provideLayoutRes() {
        return R.layout.wheel_picker_time;
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public List<NumberWheelView> provideWheelViews() {
        return Arrays.asList(this.hourWheelView, this.minuteWheelView, this.secondWheelView);
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public void onInit(Context context) {
        this.hourWheelView = (NumberWheelView) findViewById(R.id.wheel_picker_time_hour_wheel);
        this.minuteWheelView = (NumberWheelView) findViewById(R.id.wheel_picker_time_minute_wheel);
        this.secondWheelView = (NumberWheelView) findViewById(R.id.wheel_picker_time_second_wheel);
        this.hourLabelView = (TextView) findViewById(R.id.wheel_picker_time_hour_label);
        this.minuteLabelView = (TextView) findViewById(R.id.wheel_picker_time_minute_label);
        this.secondLabelView = (TextView) findViewById(R.id.wheel_picker_time_second_label);
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public void onAttributeSet(Context context, AttributeSet attributeSet) {
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.TimeWheelLayout);
        setTimeMode(obtainStyledAttributes.getInt(R.styleable.TimeWheelLayout_wheel_timeMode, 0));
        String string = obtainStyledAttributes.getString(R.styleable.TimeWheelLayout_wheel_hourLabel);
        String string2 = obtainStyledAttributes.getString(R.styleable.TimeWheelLayout_wheel_minuteLabel);
        String string3 = obtainStyledAttributes.getString(R.styleable.TimeWheelLayout_wheel_secondLabel);
        obtainStyledAttributes.recycle();
        setTimeLabel(string, string2, string3);
        setTimeFormatter(new UnitTimeFormatter());
    }

    /* access modifiers changed from: protected */
    public void onVisibilityChanged(View view, int i) {
        super.onVisibilityChanged(view, i);
        if (i == 0 && this.startValue == null && this.endValue == null) {
            setRange(TimeEntity.target(0, 0, 0), TimeEntity.target(23, 59, 59), TimeEntity.now());
        }
    }

    @Override // com.youyi.yyviewsdklibrary.picker.contract.OnWheelChangedListener
    public void onWheelSelected(WheelView wheelView, int i) {
        int id = wheelView.getId();
        if (id == R.id.wheel_picker_time_hour_wheel) {
            Integer num = (Integer) this.hourWheelView.getItem(i);
            this.selectedHour = num;
            if (this.resetWhenLinkage) {
                this.selectedMinute = null;
                this.selectedSecond = null;
            }
            changeMinute(num.intValue());
            timeSelectedCallback();
        } else if (id == R.id.wheel_picker_time_minute_wheel) {
            this.selectedMinute = (Integer) this.minuteWheelView.getItem(i);
            if (this.resetWhenLinkage) {
                this.selectedSecond = null;
            }
            changeSecond();
            timeSelectedCallback();
        } else if (id == R.id.wheel_picker_time_second_wheel) {
            this.selectedSecond = (Integer) this.secondWheelView.getItem(i);
            timeSelectedCallback();
        }
    }

    @Override // com.youyi.yyviewsdklibrary.picker.contract.OnWheelChangedListener, com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public void onWheelScrollStateChanged(WheelView wheelView, int i) {
        int id = wheelView.getId();
        boolean z = true;
        if (id == R.id.wheel_picker_time_hour_wheel) {
            this.minuteWheelView.setEnabled(i == 0);
            NumberWheelView numberWheelView = this.secondWheelView;
            if (i != 0) {
                z = false;
            }
            numberWheelView.setEnabled(z);
        } else if (id == R.id.wheel_picker_time_minute_wheel) {
            this.hourWheelView.setEnabled(i == 0);
            NumberWheelView numberWheelView2 = this.secondWheelView;
            if (i != 0) {
                z = false;
            }
            numberWheelView2.setEnabled(z);
        } else if (id == R.id.wheel_picker_time_second_wheel) {
            this.hourWheelView.setEnabled(i == 0);
            NumberWheelView numberWheelView3 = this.minuteWheelView;
            if (i != 0) {
                z = false;
            }
            numberWheelView3.setEnabled(z);
        }
    }

    private void timeSelectedCallback() {
        if (this.onTimeSelectedListener != null) {
            this.secondWheelView.post(new Runnable() {
                /* class com.youyi.yyviewsdklibrary.picker.widget.TimeWheelLayout.AnonymousClass1 */

                public void run() {
                    TimeWheelLayout.this.onTimeSelectedListener.onTimeSelected(TimeWheelLayout.this.selectedHour.intValue(), TimeWheelLayout.this.selectedMinute.intValue(), TimeWheelLayout.this.selectedSecond.intValue());
                }
            });
        }
        if (this.onTimeMeridiemSelectedListener != null) {
            this.secondWheelView.post(new Runnable() {
                /* class com.youyi.yyviewsdklibrary.picker.widget.TimeWheelLayout.AnonymousClass2 */

                public void run() {
                    TimeWheelLayout.this.onTimeMeridiemSelectedListener.onTimeSelected(TimeWheelLayout.this.selectedHour.intValue(), TimeWheelLayout.this.selectedMinute.intValue(), TimeWheelLayout.this.selectedSecond.intValue());
                }
            });
        }
    }

    public void setTimeMode(int i) {
        this.timeMode = i;
        this.hourWheelView.setVisibility(View.VISIBLE);
        this.hourLabelView.setVisibility(View.VISIBLE);
        this.minuteWheelView.setVisibility(View.VISIBLE);
        this.minuteLabelView.setVisibility(View.VISIBLE);
        this.secondWheelView.setVisibility(View.VISIBLE);
        this.secondLabelView.setVisibility(View.VISIBLE);
        if (i == -1) {
            this.hourWheelView.setVisibility(View.GONE);
            this.hourLabelView.setVisibility(View.GONE);
            this.minuteWheelView.setVisibility(View.GONE);
            this.minuteLabelView.setVisibility(View.GONE);
            this.secondWheelView.setVisibility(View.GONE);
            this.secondLabelView.setVisibility(View.GONE);
            this.timeMode = i;
        } else if (i == 2 || i == 0) {
            this.secondWheelView.setVisibility(View.GONE);
            this.secondLabelView.setVisibility(View.GONE);
        }
    }

    public boolean isHour12Mode() {
        int i = this.timeMode;
        return i == 2 || i == 3;
    }

    public void setRange(TimeEntity timeEntity, TimeEntity timeEntity2) {
        setRange(timeEntity, timeEntity2, null);
    }

    public void setRange(TimeEntity timeEntity, TimeEntity timeEntity2, TimeEntity timeEntity3) {
        boolean z = false;
        if (timeEntity == null) {
            timeEntity = TimeEntity.target(isHour12Mode() ? 1 : 0, 0, 0);
        }
        if (timeEntity2 == null) {
            timeEntity2 = TimeEntity.target(isHour12Mode() ? 12 : 23, 59, 59);
        }
        if (timeEntity2.toTimeInMillis() >= timeEntity.toTimeInMillis()) {
            this.startValue = timeEntity;
            this.endValue = timeEntity2;
            if (timeEntity3 == null) {
                timeEntity3 = timeEntity;
            }
            this.defaultValue = timeEntity3;
            if (timeEntity3.getHour() < 12 || timeEntity3.getHour() == 24) {
                z = true;
            }
            this.isAnteMeridiem = z;
            this.selectedHour = Integer.valueOf(fixHour(timeEntity3.getHour()));
            this.selectedMinute = Integer.valueOf(timeEntity3.getMinute());
            this.selectedSecond = Integer.valueOf(timeEntity3.getSecond());
            changeHour();
            return;
        }
        throw new IllegalArgumentException("Ensure the start time is less than the time date");
    }

    public void setDefaultValue(TimeEntity timeEntity) {
        setRange(this.startValue, this.endValue, timeEntity);
    }

    public void setTimeFormatter(final TimeFormatter timeFormatter) {
        if (timeFormatter != null) {
            this.hourWheelView.setFormatter(new WheelFormatter() {
                /* class com.youyi.yyviewsdklibrary.picker.widget.TimeWheelLayout.AnonymousClass3 */

                @Override // com.youyi.yyviewsdklibrary.picker.contract.WheelFormatter
                public String formatItem(Object obj) {
                    return timeFormatter.formatHour(((Integer) obj).intValue());
                }
            });
            this.minuteWheelView.setFormatter(new WheelFormatter() {
                /* class com.youyi.yyviewsdklibrary.picker.widget.TimeWheelLayout.AnonymousClass4 */

                @Override // com.youyi.yyviewsdklibrary.picker.contract.WheelFormatter
                public String formatItem(Object obj) {
                    return timeFormatter.formatMinute(((Integer) obj).intValue());
                }
            });
            this.secondWheelView.setFormatter(new WheelFormatter() {
                /* class com.youyi.yyviewsdklibrary.picker.widget.TimeWheelLayout.AnonymousClass5 */

                @Override // com.youyi.yyviewsdklibrary.picker.contract.WheelFormatter
                public String formatItem(Object obj) {
                    return timeFormatter.formatSecond(((Integer) obj).intValue());
                }
            });
        }
    }

    public void setTimeLabel(CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3) {
        this.hourLabelView.setText(charSequence);
        this.minuteLabelView.setText(charSequence2);
        this.secondLabelView.setText(charSequence3);
    }

    public void setOnTimeSelectedListener(OnTimeSelectedListener onTimeSelectedListener2) {
        this.onTimeSelectedListener = onTimeSelectedListener2;
    }

    public void setOnTimeMeridiemSelectedListener(OnTimeMeridiemSelectedListener onTimeMeridiemSelectedListener2) {
        this.onTimeMeridiemSelectedListener = onTimeMeridiemSelectedListener2;
    }

    public void setResetWhenLinkage(boolean z) {
        this.resetWhenLinkage = z;
    }

    public void setTimeStep(int i, int i2, int i3) {
        this.hourStep = i;
        this.minuteStep = i2;
        this.secondStep = i3;
        if (isDataAlready()) {
            setRange(this.startValue, this.endValue, this.defaultValue);
        }
    }

    /* access modifiers changed from: protected */
    public boolean isDataAlready() {
        return (this.startValue == null || this.endValue == null) ? false : true;
    }

    public final TimeEntity getStartValue() {
        return this.startValue;
    }

    public final TimeEntity getEndValue() {
        return this.endValue;
    }

    public final NumberWheelView getHourWheelView() {
        return this.hourWheelView;
    }

    public final NumberWheelView getMinuteWheelView() {
        return this.minuteWheelView;
    }

    public final NumberWheelView getSecondWheelView() {
        return this.secondWheelView;
    }

    public final TextView getHourLabelView() {
        return this.hourLabelView;
    }

    public final TextView getMinuteLabelView() {
        return this.minuteLabelView;
    }

    public final TextView getSecondLabelView() {
        return this.secondLabelView;
    }

    @Deprecated
    public final TextView getMeridiemLabelView() {
        throw new UnsupportedOperationException("Use getMeridiemWheelView instead");
    }

    public final int getSelectedHour() {
        return fixHour(((Integer) this.hourWheelView.getCurrentItem()).intValue());
    }

    private int fixHour(int i) {
        if (!isHour12Mode()) {
            return i;
        }
        if (i == 0) {
            i = 24;
        }
        return i > 12 ? i - 12 : i;
    }

    public final int getSelectedMinute() {
        return ((Integer) this.minuteWheelView.getCurrentItem()).intValue();
    }

    public final int getSelectedSecond() {
        int i = this.timeMode;
        if (i == 2 || i == 0) {
            return 0;
        }
        return ((Integer) this.secondWheelView.getCurrentItem()).intValue();
    }

    private void changeHour() {
        int min = Math.min(this.startValue.getHour(), this.endValue.getHour());
        int max = Math.max(this.startValue.getHour(), this.endValue.getHour());
        boolean isHour12Mode = isHour12Mode();
        int i = isHour12Mode() ? 12 : 23;
        int max2 = Math.max(isHour12Mode ? 1 : 0, min);
        int min2 = Math.min(i, max);
        Integer num = this.selectedHour;
        if (num == null) {
            this.selectedHour = Integer.valueOf(max2);
        } else {
            Integer valueOf = Integer.valueOf(Math.max(num.intValue(), max2));
            this.selectedHour = valueOf;
            this.selectedHour = Integer.valueOf(Math.min(valueOf.intValue(), min2));
        }
        this.hourWheelView.setRange(max2, min2, this.hourStep);
        this.hourWheelView.setDefaultValue(this.selectedHour);
        changeMinute(this.selectedHour.intValue());
    }

    private void changeMinute(int i) {
        int i2 = 0;
        int i3 = 59;
        if (i == this.startValue.getHour() && i == this.endValue.getHour()) {
            i2 = this.startValue.getMinute();
            i3 = this.endValue.getMinute();
        } else if (i == this.startValue.getHour()) {
            i2 = this.startValue.getMinute();
        } else if (i == this.endValue.getHour()) {
            i3 = this.endValue.getMinute();
        }
        Integer num = this.selectedMinute;
        if (num == null) {
            this.selectedMinute = Integer.valueOf(i2);
        } else {
            Integer valueOf = Integer.valueOf(Math.max(num.intValue(), i2));
            this.selectedMinute = valueOf;
            this.selectedMinute = Integer.valueOf(Math.min(valueOf.intValue(), i3));
        }
        this.minuteWheelView.setRange(i2, i3, this.minuteStep);
        this.minuteWheelView.setDefaultValue(this.selectedMinute);
        changeSecond();
    }

    private void changeSecond() {
        if (this.selectedSecond == null) {
            this.selectedSecond = 0;
        }
        this.secondWheelView.setRange(0, 59, this.secondStep);
        this.secondWheelView.setDefaultValue(this.selectedSecond);
    }
}
